//
//  DeviceListViewController.m
//  OST
//
//  Created by mac on 13-7-5.
//  Copyright (c) 2013年 mac. All rights reserved.
//

#import "DeviceListViewController.h"

#import "DeviceListCell.h"
#import "AutomationViewController.h"
#import "CameraListViewController.h"
#import "MeterListViewController.h"
#import "RemoteViewController.h"
#import "SceneViewController.h"
#import "FamilyListViewController.h"
#import "Common.h"

#import "LayerData.h"
#import "CommonData.h"

#import "SetData.h"
#import "CameraData.h"
#import "FamilyData.h"
#import "SceneData.h"


@interface DeviceListViewController ()

@end

@implementation DeviceListViewController
@synthesize type;

- (void)viewDidLoad
{
    [super viewDidLoad];
    closeNextPage = YES;
    threadLoadData = NO;
    
    
//    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
//                                                    initWithTarget:self
//                                                    action:@selector(backToDecice)];
//    [self.view addGestureRecognizer:panGestureRecognizer];
    //CGFloat h = self.view.frame.size.height;
    
    BottomView *bottomView =  [self addBottomView];
    bottomView.btn_camera.hidden = YES;
    dataTableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - bottomView.frame.size.height);
    
    if (type == DeviceArm) {
        self.title =@"Arm";
    }else if (type == DeviceAutomation) {
        self.title =@"Automation";
    }else if (type == DeviceCamera) {
        self.title =@"Surveillance";
    }else if (type == DeviceGps) {
        self.title =@"Gps";
    }else if (type == DeviceMeter) {
        self.title =@"Meter";
    }else if (type == DeviceRemote) {
        self.title =@"Remote";
    }else if (type == DeviceScense) {
        self.title =@"Scense";
    }
    
    [self refreshData];
}

- (void)backToDecice
{
//    [self.navigationController popViewControllerAnimated:YES];
}

-(CGFloat)rowHeight
{
    return 76;
}

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSUInteger row=[indexPath row];
    static NSString *DeviceListCellIdentifier = @"DeviceListCellIdentifier";
    DeviceListCell *cell = (DeviceListCell*)[dataTableView dequeueReusableCellWithIdentifier:DeviceListCellIdentifier];
    if (cell==nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"DeviceListCell" owner:self options:nil]lastObject];
        [cell initView];
        cell.changeBtn.hidden =YES;
        cell.remoteChnageBtn.hidden  = YES;
        cell.btn_refresh.tag = row;
        [cell.btn_refresh addTarget:self action:@selector(reloadData:) forControlEvents:UIControlEventTouchUpInside];
        
        if (type == DeviceArm) {
            cell.icon.image = [UIImage imageNamed:@"icon_arm"];
            cell.btn_refresh.hidden = YES;
            cell.btn_arm.hidden = NO;
            cell.btn_ds_arm.hidden = NO;
            [cell loadArmData];
        }else if (type == DeviceAutomation) {
            
            cell.icon.image = [UIImage imageNamed:@"icon_appliance"];
            cell.icon.highlightedImage = [UIImage imageNamed:@"icon_appliance_seleced"];
            
        }else if (type == DeviceCamera) {
            cell.icon.image = [UIImage imageNamed:@"icon_camera"];
            cell.icon.highlightedImage = [UIImage imageNamed:@"icon_camera_selected"];
        }else if (type == DeviceGps) {
            cell.icon.image = [UIImage imageNamed:@"gps_position"];
        }else if (type == DeviceMeter) {
            cell.icon.image = [UIImage imageNamed:@"icon_ammeter"];
        }else if (type == DeviceRemote) {
            cell.icon.image = [UIImage imageNamed:@"icon_infrared"];
        }else if (type == DeviceScense) {
            cell.icon.image = [UIImage imageNamed:@"icon_scene"];
        }
    }
    NSDictionary *rowData = [self.rows objectAtIndex:row];
    cell.labelName.text = [rowData objectForKey:@"alias"];
    return cell;
}


-(void)reloadData:(UIButton *)sender
{
    //int index = sender.tag;
    [activity startAnimating];
    if (type == DeviceAutomation || type == DeviceMeter || type == DeviceRemote) {
        [[SetData sharedInstance]reLoadDataWithDataRecivedBlock:^(id data) {
            [activity stopAnimating];
        }];
    }else if (type == DeviceCamera) {
        [[CameraData sharedInstance]reLoadDataWithDataRecivedBlock:^(id data) {
            [activity stopAnimating];
        }];
    }else if (type == DeviceGps) {
        [[FamilyData sharedInstance]reLoadDataWithDataRecivedBlock:^(id data) {
            [activity stopAnimating];
        }];
    }else if (type == DeviceScense) {
        [[SceneData sharedInstance]reLoadDataWithDataRecivedBlock:^(id data) {
            [activity stopAnimating];
        }];
    }
    
}


-(void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (type == DeviceAutomation) {
        AutomationViewController *autom = [[AutomationViewController alloc] init];
        autom.title = @"Automation";
        [self.navigationController pushViewController:autom animated:YES];
    }else if (type == DeviceCamera){
        CameraListViewController *camer = [[CameraListViewController alloc] init];
        [self.navigationController pushViewController:camer animated:YES];
    }else if (type == DeviceMeter){
        MeterListViewController *camer = [[MeterListViewController alloc] init];
        camer.title = @"Meter";
        [self.navigationController pushViewController:camer animated:YES];
    }else if (type == DeviceRemote){
        RemoteViewController *camer = [[RemoteViewController alloc] init];
        [self.navigationController pushViewController:camer animated:YES];
    }else if (type ==  DeviceScense){
        SceneViewController *scene = [[SceneViewController alloc]init];
        [self.navigationController pushViewController:scene animated:YES];
    }else if (type ==  DeviceGps){
        FamilyListViewController *family = [[FamilyListViewController alloc] init];
        [self.navigationController pushViewController:family animated:YES];
    }
}





-(NSArray *)loadPageData:(int)page
{
    [activity startAnimating];
    [[LayerData sharedInstance]getDataWithDataRecivedBlock:^(id data) {
        [activity stopAnimating];
        [super doneLoadingTableViewData:data];
    }];
    
    return nil;
}



@end
